﻿create procedure [dbo].[usp_ProcessUpdate]
	@ProcessId int
	, @ConcurrencyId rowversion
	, @BasePriority int
	, @ExitCode int
	, @ExitTime datetime2
	, @HasExited bit
	, @HandleCount int
	, @Id int
	, @MachineName nvarchar(100)
	, @MainModule nvarchar(512)
	, @MainWindowTitle nvarchar(100)
	, @MaxWorkingSet bigint
	, @MinWorkingSet bigint
	, @NonpagedSystemMemorySize64 bigint
	, @PagedMemorySize64 bigint
	, @PagedSystemMemorySize64 bigint
	, @PeakPagedMemorySize64 bigint
	, @PeakVirtualMemorySize64 bigint
	, @PeakWorkingSet64 bigint
	, @PriorityBoostEnabled bit
	, @PriorityClass nvarchar(100)
	, @PrivateMemorySize64 bigint
	, @PrivilegedProcessorTime time
	, @ProcessName nvarchar(100)
	, @ProcessorAffinity bigint
	, @Responding bit
	, @SessionId int
	, @StartTime datetime2
	, @Threads int
	, @TotalProcessorTime time
	, @UserProcessorTime time
	, @VirtualMemorySize64 bigint
	, @WorkingSet64 bigint
as

begin try
	
	begin transaction
	
		update [dbo].[Process]
		set
			[BasePriority] = @BasePriority
			, [ExitCode] = @ExitCode
			, [ExitTime] = @ExitTime
			, [HasExited] = @HasExited
			, [HandleCount] = @HandleCount
			, [Id] = @Id
			, [MachineName] = @MachineName
			, [MainModule] = @MainModule
			, [MainWindowTitle] = @MainWindowTitle
			, [MaxWorkingSet] = @MaxWorkingSet
			, [MinWorkingSet] = @MinWorkingSet
			, [NonpagedSystemMemorySize64] = @NonpagedSystemMemorySize64
			, [PagedMemorySize64] = @PagedMemorySize64
			, [PagedSystemMemorySize64] = @PagedSystemMemorySize64
			, [PeakPagedMemorySize64] = @PeakPagedMemorySize64
			, [PeakVirtualMemorySize64] = @PeakVirtualMemorySize64
			, [PeakWorkingSet64] = @PeakWorkingSet64
			, [PriorityBoostEnabled] = @PriorityBoostEnabled
			, [PriorityClass] = @PriorityClass
			, [PrivateMemorySize64] = @PrivateMemorySize64
			, [PrivilegedProcessorTime] = @PrivilegedProcessorTime
			, [ProcessName] = @ProcessName
			, [ProcessorAffinity] = @ProcessorAffinity
			, [Responding] = @Responding
			, [SessionId] = @SessionId
			, [StartTime] = @StartTime
			, [Threads] = @Threads
			, [TotalProcessorTime] = @TotalProcessorTime
			, [UserProcessorTime] = @UserProcessorTime
			, [VirtualMemorySize64] = @VirtualMemorySize64
			, [WorkingSet64] = @WorkingSet64
		where ProcessId = @ProcessId and ConcurrencyId = @ConcurrencyId
		
		-- if the operating system process has exited, update process exit details for each row representing the same operating system process. 
		if @HasExited = 1
		begin
			update [dbo].[Process]
			set
			    [ExitCode] = @ExitCode
			    , [ExitTime] = @ExitTime
			    , [HasExited] = @HasExited
			where [Id] = @Id and [StartTime] = @StartTime
		end
		
	commit transaction

end try
	
begin catch
	if @@trancount > 0
	begin
		rollback transaction;
	end
	execute [dbo].[usp_SqlErrorInsert]
end catch